from typing import Optional
import uvicorn
from fastapi import FastAPI

from loguru import logger

from pyfirmata import Arduino
board = Arduino('COM6')

app = FastAPI()


@app.get("/")
def read_root():
    logger.info("开灯")
    board.digital[12].write(1)  # 向端口13写入0   0代表灭灯
    board.digital[13].write(0)  # 向端口13写入0   0代表灭灯
    return {"status": "开灯"}


@app.get("/shutdown")
def read_item():
    logger.info("关灯")
    board.digital[12].write(0)  # 向端口13写入1   1代表亮灯
    board.digital[13].write(1)  # 向端口13写入1   1代表亮灯
    return {"status": "关灯"}


if __name__ == '__main__':
    logger.debug("程序开始启动")
    uvicorn.run(app=app, host="0.0.0.0", port=8000)
